<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
  <head>
    
    <title>员工信息表</title>
	<meta name="pragma" content="no-cache">
	<meta name="cache-control" content="no-cache">
	<meta name="expires" content="0">    
    <div th:replace="~{commons/script::script}"></div>

  </head>
  <style type="text/css">
  body{
  	font-size:14px
  }
  </style>
  <body >
    <div id="toolbar" style="padding:0 30px;">
       	<b>员工姓名: </b><input class="easyui-textbox" id="name" type="text" name="name" style="width:166px;height:35px;line-height:35px;"/>
        <a href="javascript:void(0);" id="searchBtn" class="easyui-linkbutton" iconCls="icon-search" data-options="selected:true" style="background-color:#00aa00;color:white">查询</a>
        <a href="javascript:void(0);" id="setBtn" class="easyui-linkbutton" iconCls="icon-reload" style="background-color:#00aa00;color:white;margin-left:10px">重置</a>
        <a href="javascript:void(0);" onclick="return add('/employee/toAddEmployee')" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" style="background-color:#00aa00;color:white;margin-left:10px">添加</a>
    	<a href="javascript:void(0);" onclick="return remove('/employee/deleteEmployee')" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" style="background-color:#f00;color:white;margin-left:10px">删除</a>
    	<a href="javascript:void(0);" onclick="return edit('/employee/toEditEmployee')" id="editBtn" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" style="background-color:#00aa00;color:white;margin-left:10px">修改</a>
    </div>
    <table id="empList" class="easyui-datagrid" style="font-size:14px"></table>
    <script type="text/javascript">
    	//为查询按钮添加处理函数
	    $("#searchBtn").on("click",function(){
			var name = $("#name").val();
            $("#name").val("")
			$("#empList").datagrid("load",{"name":name});
			
		})
		//为添加按钮添加事件处理函数
		function add(url){
    		parent.openTopWindow({
    			title:"添加员工信息",
    			width:550,
    			height:500,
    			"url" : url,
    			close : function(){
    				$("#empList").datagrid("reload");
    			}
    		});
    	}
	 //为重置按钮添加事件处理函数
       $("#setBtn").on("click",function(){
       		$("#empList").datagrid("load",{"emp.name":""});
       })
  
    	//修改按钮事件处理函数
      	function edit(url){
    		var rows = $("#empList").datagrid("getChecked");
    		if(!rows.length){
    			$.messager.alert("提示","请选择需要修改的权限信息");
    			return false;
    		}
    		
    		if(rows.length >= 2){
    			$.messager.alert("提示","一次只能选择修改一条产品信息");
    			return false;
    		}
    		
    		var empId = rows[0].id;
    		parent.openTopWindow({
    			title:"修改员工信息",
    			width:550,
    			height:500,
    			"url" : url+"?id="+empId,
    			close : function(){
    				$("#empList").datagrid("reload");
    			}
    		});
    	}
    	
        //删除按钮事件处理函数
        function remove(url){
        	//获取到选中的一行数据
    			var rows = $("#empList").datagrid("getChecked");
    			
    			if(!rows.length){
    				$.messager.alert("警告","请选择要删除的数据");
    				return;
    			}
    			
    			$.messager.confirm("警告","数据删除后无法恢复，是否确认删除",function(b){
    				if(b){
    					var ids = new Array();
    					$.each(rows,function(index,obj){
    						ids.push(obj.id);
    					});
    					//将数组中元素使用,分割拼接为一个字符串
    					ids = ids.join(",");
    					$.post(url,
    						{
    							"ids" : ids
    						},
    						function(data){
    							if(data && data.success){
    								$.messager.alert("提示",data.message);
    								$("#empList").datagrid("reload");
    								return;
    							}
    							$.messager.alert("警告",data.message);
    					},"json");
    				}
    			});
        }
    	//加载用户数据
    	$(function(){
    		$("#empList").datagrid({
    			url : "/employee/listEmployee",
    			pagination : true,
    			toolbar : "#toolbar",
    			fitColumns : true,
    			idField : "id",
    			rownumbers : true,
    			//singleSelect:true,
    			columns : [
                    [
    				{field:"id",title:"选择",checkbox:true},
    				{field:"name",title:"员工姓名",sortable:true,width:10},
                    {field:"position",title:"岗位",sortable:true,width:10},
                    {field:"status",title:"状态",sortable:true,width:10,formatter:function(value,rowData,index){
                        if(value==1){
                            return "激活";
                        }else{
                            return "未激活";
                        }
                        }},
                    {field:"userName",title:"对应的用户",sortable:true,width:10},
                    {field:"sex",title:"性别",sortable:true,width:10,formatter:function(value,rowData,index){
                        if(value==1){
                            return "男";
                        }else{
                            return "女";
                        }
                        }},
    				{field:"telephone",title:"联系电话",sortable:true,width:10},
    				{field:"email",title:"邮箱",sortable:true,width:10},
    				{field:"address",title:"地址",width:10},			
    			]
                ],
    			loadFilter:function(data){
    				//data是服务器返回的数据,将服务器端返回的数据转换为datagrid需要的格式
    				return {"total":data.data.totalRows,"rows":data.data.result};
    			}
    		
    		});
    	});
    </script>
  </body>
</html>
